<template>
  <el-dialog title="教师列表" :visible.sync="visible" width="1000px" append-to-body :before-close="handleClose">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true"  label-width="68px">
      <el-form-item label="教师名称" prop="teacherName">
        <el-input
          v-model="queryParams.teacherName"
          placeholder="请输入教师名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="手机号" prop="teacherPhone">
        <el-input
          v-model="queryParams.teacherPhone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      ref="infoTable"
      v-loading="loading"
      :data="infoList"
      @select="handleSelect"
      @select-all="handleSelectAll"
      stripe
      border
      height="400px"
    >
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="教师名称" align="center" prop="teacherName"/>
      <el-table-column label="毕业学校" align="center" prop="school"/>
      <el-table-column label="性别" align="center" prop="gender">
        <template v-slot:default="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.gender"/>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="teacherPhone"/>
      <el-table-column label="入职时间" align="center" prop="employmentDate" width="180">
        <template v-slot:default="scope">
          <span>{{ parseTime(scope.row.employmentDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listInfo } from '@/api/edu/teacher'

export default {
  name: 'SelectTeacherDialog',
  dicts: ['sys_user_sex'],
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      loading: true,
      total: 0,
      infoList: [],
      selectedTeacherRow: null,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teacherName: null,
        teacherPhone: null,
        isEmployment:"Y"
      }
    }
  },
  watch: {
    visible(newVal) {
      if (newVal) {
        this.getList()
      }
    }
  },
  methods: {
    getList() {
      this.loading = true
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleSelect(selection, row) {
      this.$refs.infoTable.clearSelection()
      if (selection.length > 0) {
        this.$refs.infoTable.toggleRowSelection(row, true)
        this.selectedTeacherRow = row
      }
    },
    handleSelectAll(selection) {
      if (selection.length > 0) {
        this.$refs.infoTable.clearSelection()
        this.selectedTeacherRow = null
      }
    },
    submit() {
      if (!this.selectedTeacherRow) {
        this.$message.warning('请先选择一名教师')
        return
      }
      this.$emit('select', this.selectedTeacherRow)
      this.$emit('update:visible', false)
      this.reset()
    },
    cancel() {
      this.$emit('update:visible', false)
      this.reset()
    },
    reset() {
      this.selectedTeacherRow = null
      if (this.$refs.infoTable) {
        this.$refs.infoTable.clearSelection()
      }
    },
    // 关键修复：处理右上角关闭按钮
    handleClose(done) {
      this.$emit('update:visible', false)
      this.reset()
      done()
    }
  }
}
</script>

<style scoped>
.dialog-footer {
  text-align: center;
}
</style>
